﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.IO;
using System.Web.UI;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Modules.UserDefinedTable.Security;
using DotNetNuke.Security;
using DotNetNuke.Services.Localization;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public partial class Default : PortalModuleBase, IActionable
  {


    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);

      this.InitViews();
      //base.Load += new EventHandler(this.Page_Init);
    }




    private void InitViews()
    {
      ModuleSecurity security = new ModuleSecurity(this.ModuleId, this.TabId, this.Settings);

      switch (Utilities.CStrN((this.Settings[SettingName.ListOrForm]), "Unknown"))
      {
        case "List":
          this.LoadControlByKey("List");
          break;

        case "Form":
          if ((Utilities.CStrN(this.Request.QueryString["show"], "") == "records") && security.IsAllowedToViewList())
            this.LoadControlByKey("List");
          
          else
            this.LoadControlByKey("Edit");
          
          break;

        case "FormAndList":
          this.LoadControlByKey("Edit");
          if (security.IsAllowedToViewList())
          {
            this.Controls.Add(new LiteralControl("<br/>"));
            this.LoadControlByKey("List");
          }
          break;

        case "ListAndForm":
          if (security.IsAllowedToViewList())
          {
            this.LoadControlByKey("List");
            this.Controls.Add(new LiteralControl("<br/>"));
          }
          this.LoadControlByKey("Edit");
          break;

        default:
          if (this.IsNewModuleInstance())
            this.LoadControlByKey("TemplateList");
          
          else
            this.LoadControlByKey("List");
          
          break;
      }
    }




    public bool IsNewModuleInstance()
    {
      Hashtable settings = this.ModuleContext.Settings;
      ModuleController controller = new ModuleController();
      int num = 0;

      if (settings.ContainsKey(SettingName.ExcludeFromSearch))
        num++;
      
      if (settings.ContainsKey(SettingName.CalculatedColumnsRenderExpressionInForm))
        num++;
      
      if (settings.Count > num)
        return false;
      
      controller.UpdateModuleSetting(this.ModuleId, SettingName.ExcludeFromSearch, bool.TrueString);
      controller.UpdateModuleSetting(this.ModuleId, SettingName.CalculatedColumnsRenderExpressionInForm, bool.TrueString);

      return true;
    }




    private void LoadControlByKey(string ControlKey)
    {
      ModuleControlInfo moduleControlByControlKey = ModuleControlController.GetModuleControlByControlKey(ControlKey, this.ModuleContext.Configuration.ModuleDefID);
      if (moduleControlByControlKey != null)
      {
        string virtualPath = string.Format("~/{0}", moduleControlByControlKey.ControlSrc);
        PortalModuleBase child = (PortalModuleBase)this.LoadControl(virtualPath);
        child.ID = Path.GetFileNameWithoutExtension(virtualPath);
        child.ModuleContext.Configuration = this.ModuleContext.Configuration;
        this.Controls.Add(child);
      }
    }





    ModuleActionCollection DotNetNuke.Entities.Modules.IActionable.ModuleActions
    {
      get
      {
        string str = Utilities.CStrN((this.Settings[SettingName.ListOrForm]), "Undefined");
        ModuleActionCollection actions = new ModuleActionCollection();
        ModuleActionCollection moduleActions;

        foreach(Control current in this.Controls)
        {
          if (current is IActionable)
          {
            moduleActions = ((IActionable)current).ModuleActions;

            foreach(ModuleAction action in moduleActions)
            {
              action.ID = this.GetNextActionID();
              actions.Add(action);
            }
          }
        }

        actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ContentOptions.Action", this.LocalResourceFile), "ModuleSettings.Action", "", "settings.gif", this.EditUrl("Manage"), false, SecurityAccessLevel.Edit, true, false);

        if (str != "Undefined")
          actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("CreateTemplate.Action", this.LocalResourceFile), "CreateTemplate", "", this.ResolveUrl("~/images/eip_save.gif"), this.EditUrl("CreateTemplate"), false, SecurityAccessLevel.Admin, true, false);

        actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ExportCSV.Action", this.LocalResourceFile), "ExportModule.Action", "", this.ResolveUrl("~/images/action_export.gif"), this.EditUrl("", "", "ExportCSV", new string[] { "moduleid=" + this.ModuleId.ToString() }), false, SecurityAccessLevel.Edit, true, false);
        actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ImportCSV.Action", this.LocalResourceFile), "ImportModule.Action", "", this.ResolveUrl("~/images/action_import.gif"), this.EditUrl("", "", "ImportCSV", new string[] { "moduleid=" + this.ModuleId.ToString() }), false, SecurityAccessLevel.Edit, true, false);
        return actions;
      }
    }





    public ModuleActionCollection ModuleActions
    {
      get
      {
        string str = Utilities.CStrN((this.Settings[SettingName.ListOrForm]), "Undefined");
        ModuleActionCollection actions = new ModuleActionCollection();

        foreach( Control current in this.Controls)
        {
          if (current is IActionable)
          {
            foreach (ModuleAction action in ((IActionable)current).ModuleActions)
            {
              action.ID = this.GetNextActionID();
              actions.Add(action);
            }
          }
        }
        actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ContentOptions.Action", this.LocalResourceFile), "ModuleSettings.Action", "", "settings.gif", this.EditUrl("Manage"), false, SecurityAccessLevel.Edit, true, false);
        
        if (str != "Undefined")
          actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("CreateTemplate.Action", this.LocalResourceFile), "CreateTemplate", "", this.ResolveUrl("~/images/eip_save.gif"), this.EditUrl("CreateTemplate"), false, SecurityAccessLevel.Admin, true, false);
        
        actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ExportCSV.Action", this.LocalResourceFile), "ExportModule.Action", "", this.ResolveUrl("~/images/action_export.gif"), this.EditUrl("", "", "ExportCSV", new string[] { "moduleid=" + this.ModuleId.ToString() }), false, SecurityAccessLevel.Edit, true, false);
        actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ImportCSV.Action", this.LocalResourceFile), "ImportModule.Action", "", this.ResolveUrl("~/images/action_import.gif"), this.EditUrl("", "", "ImportCSV", new string[] { "moduleid=" + this.ModuleId.ToString() }), false, SecurityAccessLevel.Edit, true, false);

        return actions;
      }
    }


  }

}


